//
//  XMPicwordCommentHeadView.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/11.
//

#import "XMPicwordCommentHeadView.h"

@interface XMPicwordCommentHeadView ()

@property (nonatomic, strong) UIButton *bgBtn;
@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *dateLabel;

@property (nonatomic, strong) UIButton *likeBtn;
@property (nonatomic, strong) UILabel *likeNumLabel;

@end

@implementation XMPicwordCommentHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
//    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
//    if (self) {
//        [self creatUI];
//    }
//    return self;
//}

- (void)creatUI {
    UIButton *bgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bgBtn.frame = self.bounds;
    [bgBtn addTarget:self action:@selector(commentAction:) forControlEvents:UIControlEventTouchUpInside];
    _bgBtn = bgBtn;
    [self addSubview:bgBtn];
    [self addSubview:self.avatarImgView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.contentLabel];
    [self addSubview:self.dateLabel];
    
    [self addSubview:self.likeBtn];
}

- (void)setContentWithCommentModel:(XMPicwordCommentModel *)commentModel {
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:commentModel.userAvatar] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    _nameLabel.text = commentModel.userNickName;
    
    CGFloat height = [commentModel.content getHeightWithFont:KScaleWidth(14.0) withWidth:_contentLabel.width] + KScaleWidth(5.0);
    _contentLabel.height = height;
    _contentLabel.text = commentModel.content;
    _dateLabel.y = _contentLabel.maxY + KScaleWidth(5.0);
    _likeBtn.y = _dateLabel.y;
    [_likeBtn setTitle:commentModel.likeCount forState:UIControlStateNormal];
    _likeBtn.selected = commentModel.likeed;
    [_likeBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleLeft ImageTitleSpace:KScaleWidth(2.0)];
    NSString *dateStr = [[NSDate dateWithTimeIntervalSince1970:[commentModel.createTime integerValue]/1000] dateStringWithFormat:@"MM-dd HH:mm"];
    _dateLabel.text = [NSString stringWithFormat:@"%@", dateStr];
    
    self.height = _dateLabel.maxY + KScaleWidth(26.0);
    _bgBtn.frame = self.bounds;
}

- (void)likeAction:(UIButton *)button {
    self.likeBlock ? self.likeBlock() : nil;
}

- (void)commentAction:(UIButton *)button {
    self.commentBlock ? self.commentBlock() : nil;
}

#pragma mark - Init
- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), KScaleWidth(10.0), KScaleWidth(38.0), KScaleWidth(38.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(6.0), _avatarImgView.y, kScreenWidth - _avatarImgView.maxX - KScaleWidth(6.0) - KScaleWidth(10.0), KScaleWidth(20.0)) font:KFontSize(14.0) textColor:kColorHEX(0x666666)];
        _nameLabel.userInteractionEnabled = NO;
    }
    return _nameLabel;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.x, _nameLabel.maxY, _nameLabel.width, KScaleWidth(20.0)) font:KFontSize(14.0) textColor:kColorHEX(0x333333)];
        _contentLabel.userInteractionEnabled = NO;
    }
    return _contentLabel;
}

- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.contentLabel.x, _contentLabel.maxY + KScaleWidth(5.0), _contentLabel.width/2, KScaleWidth(18.0)) font:KFontSize(13.0) textColor:kColorHEX(0x9B9696)];
        _dateLabel.userInteractionEnabled = NO;
    }
    return _dateLabel;
}

- (UIButton *)likeBtn {
    if (!_likeBtn) {
        _likeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(kScreenWidth - KScaleWidth(84.0), self.dateLabel.y, KScaleWidth(30.0), _dateLabel.height) title:0 titleColor:kColorHEX(0x333333)];
        [_likeBtn setImage:ImageNamed(@"find_comment_like_icon") forState:UIControlStateNormal];
        [_likeBtn setImage:ImageNamed(@"find_comment_like_icon_select") forState:UIControlStateSelected];
        [_likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _likeBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
